<template>
  <section class="w-activitystatistics-main">
    <div class="box-card">
      <el-row :gutter="20">
        <el-col>
          <div class="box-card-content">
            <span>已发人数</span>
            <span class="din-front">{{ topInfo.giveNum||'0' }}</span>
          </div>
        </el-col>
        <el-col>
          <div class="box-card-content">
            <span>已领人数</span>
            <span class="din-front">{{ topInfo.drawNum||'0' }}</span>
          </div>
        </el-col>
        <el-col>
          <div class="box-card-content">
            <span>待领人数</span>
            <span class="din-front">{{ topInfo.waitNum||'0' }}</span>
          </div>
        </el-col>
        <el-col v-if="pageType==='yhq'">
          <div class="box-card-content">
            <span>已使用人数</span>
            <span class="din-front">{{ topInfo.useNumber ||'0' }}</span>
          </div>
        </el-col>
        <el-col v-if="pageType==='yhq'">
          <div class="box-card-content">
            <span>未使用人数</span>
            <span class="din-front">{{ topInfo.unusedNumber ||'0' }}</span>
          </div>
        </el-col>
        <el-col v-if="pageType=='flb'">
          <div class="box-card-content">
            <span>使用金额</span>
            <span class="din-front">{{ topInfo.useMoney||'0' }}</span>
          </div>
        </el-col>
        <el-col v-if="pageType=='flb'">
          <div class="box-card-content">
            <span>已退款金额</span>
            <span class="din-front">{{ topInfo.refundMoney||'0' }}</span>
          </div>
        </el-col>
      </el-row>
      <el-row v-if="pageType=='flb'" style="margin-top: 15px;" :gutter="20">
        <el-col>
          <div class="box-card-content">
            <span>已发金额</span>
            <span class="din-front">{{ topInfo.giveMoney||'0' }}</span>
          </div>
        </el-col>
        <el-col>
          <div class="box-card-content">
            <span>已领金额</span>
            <span class="din-front">{{ topInfo.drawMoney||'0' }}</span>
          </div>
        </el-col>
        <el-col>
          <div class="box-card-content">
            <span>待领金额</span>
            <span class="din-front">{{ topInfo.waitMoney||'0' }}</span>
          </div>
        </el-col>
        <el-col>
          <el-tooltip
            class="item"
            effect="dark"
            :content="'截止：'+topInfo.updateTime+'时余额为：'+topInfo.balance"
            placement="top"
          >
            <div class="box-card-content">
              <span>余额</span>
              <span class="din-front">{{ topInfo.balance || '0' }}</span>
            </div>
          </el-tooltip>
        </el-col>
      </el-row>
    </div>
    <el-collapse-transition>
      <div class="course-search separate">
        <div class="course-search-inner">
          <el-row>
            <el-col :span="12">
              <div class="search-inner-item">
                <div class="item-label">姓名</div>
                <el-input
                  v-model.trim="listQuery.userRealname"
                  style="width: 80%"
                  class="filter-item"
                  placeholder="请输入姓名"
                />
              </div>
            </el-col>
            <el-col :span="12">
              <div class="search-inner-item">
                <div class="item-label">状态</div>
                <el-select v-model="listQuery.status" style="width: 80%" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </div>
            </el-col>

          </el-row>
        </div>
        <div class="course-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="handleReset">重置</el-button>
        </div>
      </div>
    </el-collapse-transition>
    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>活动统计</span>
      </section>
      <section class="main-buttons-btns">
        <el-button v-if="pageType=='flb'" style="margin-left: 10px" @click="refundFun">退款</el-button>
        <el-button style="margin-left: 10px" @click="exportFun">导出</el-button>
        <el-button style="margin-left: 10px" type="primary" @click="backFun">返回</el-button>
      </section>
    </section>

    <section class="main-table">
      <v-table
        :list="list"
        :tag="tableKey"
        :selection="false"
        :hover-arr="['balance']"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
      />
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
  </section>
</template>
<script>
import {
  queryPage,
  getTopInfo, exportTradeMask, exportTradeNoMask, exportUserMask, exportUserNoMask
} from './../api'
import { exportSign } from '@/utils/fileImportOrExportSign'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import elDragDialog from '@/directive/el-drag-dialog'
import { exportCheck } from '@/utils/sensitiveInfoExportCheck'
import CryptoJS from '@/utils/aesJiaJM'
export default {
  name: 'Wactivitystatistics',
  components: {
    VTable,
    Pagination
  },
  directives: {
    elDragDialog
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        userRealname: '',
        status: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      topInfo: {},
      backUrl: '',
      options: [{
        value: 'GIDS01',
        label: '待发放'
      }, {
        value: 'GIDS02',
        label: '未领取'
      }, {
        value: 'GIDS03',
        label: '已领取'
      }],
      tableKey: 'w-activitystatistics-',
      pageType: 'flb',
      orderType: '0'
    }
  },
  created() {
    if (this.$route.query) {
      this.query = this.$route.query
      this.orderType = this.query.o || '0'
      if (this.query.from) {
        let obj = {}
        try {
          obj = JSON.parse(this.query.from)
        } catch (e) {
          // TODO handle the exception
        }
        this.setBackPath(obj)
      }
      const c = this.query.c
      if (c == '1' || c == '4' || c == '5' || c == '7') {
        this.pageType = 'yhq'
        this.tableKey += 'yhq'
      } else {
        this.tableKey += 'flb'
      }

      this.getList()
      this.getInfo()
    } else {
      this.$message.error('页面参数错误')
      this.$router.back()
    }
  },
  methods: {
    setBackPath(obj) {
      var groupCount = Object.getOwnPropertyNames(obj).length
      let path = ''
      let i = 1
      for (const key in obj) {
        if (key == 'from') {
          path += groupCount > 1 ? obj[key] + '?' : obj[key]
        } else {
          path += key + '=' + obj[key]
        }
        if (i != 1 && i != groupCount) {
          path += '&'
        }
        i++
      }
      this.backUrl = path == '' ? '/datacenter' : path
    },
    backFun() {
      this.$router.replace(this.backUrl)
    },
    refundFun() {
      this.$router.push({
        path: this.$filterPath('refund'),
        query: { id: this.$route.query.id, isRefund: this.topInfo.balance ? '1' : '0' }})
    },
    exportFun() {
      const query = exportSign({
        page: this.listQuery.page,
        pageSize: this.listQuery.limit,
        giveId: this.$route.query.id,
        userRealname: this.listQuery.userRealname ? this.listQuery.userRealname : '',
        status: this.listQuery.status ? this.listQuery.status : ''
      }).obj

      if (this.orderType === '2') {
        exportCheck('WELFARE_PAYMENT_STATISTICAL', this.$route.query.id, exportTradeMask, exportTradeNoMask, query, '活动统计信息.xlsx')
      } else {
        exportCheck('WELFARE_PAYMENT_STATISTICAL', this.$route.query.id, exportUserMask, exportUserNoMask, query, '活动统计信息.xlsx')
      }
    },
    filtercolumn(scope, item) {
      if (item === 'userIdnumber' || item === 'phoneNumber') {
        return CryptoJS.$aesJieM(scope.row[item])
      } else {
        return scope.row[item]
      }
    },
    handleReset() {
      this.listQuery.page = 1
      this.listQuery.userRealname = ''
      this.listQuery.status = ''
      this.getList()
      this.getInfo()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
      this.getInfo()
    },
    getInfo() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        getTopInfo({
          id: this.$route.query.id
        }).then(res => {
          loading.close()
          if (res.data.code == '200') {
            this.topInfo = res.data.data
          } else {
            this.$message.error(res.data.msg || '获取详情失败')
          }
        }).catch(error => {
          loading.close()
          reject(error)
        })
      })
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: this.listQuery.page,
        pageSize: this.listQuery.limit,
        giveId: this.$route.query.id,
        userRealname: this.listQuery.userRealname ? this.listQuery.userRealname : '',
        status: this.listQuery.status ? this.listQuery.status : ''
      }
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.list = response.data.data.rows || []
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
	@import '@/styles/mixin.scss';
	$dark_gray: #bfced6;

	.w-activitystatistics-main {
		padding: 10px;

		.el-dialog .el-dialog__body {
			max-height: 73vh !important;
		}

		.line {
			text-align: center;
		}

    .box-card {
      margin-bottom: 10px;
      text-align: center;
      background-color: #fff;
      padding: 20px;
      .el-col{
        width: 20% !important;
        .box-card-content{
          display: flex;
          flex-direction: column;
          justify-content: center;
          font-size: 16px;
          color: #666;
          border-radius: 8px;
          padding: 14px 0;
          span:last-child {
            margin-top: 10px;
            font-size: 24px;
            font-weight: bold;
            line-height: 1;
          }
        }
        &:first-child .box-card-content{
          background: #fff2f1;
          span:last-child{
            color: #E44D42;
          }
        }
        &:nth-child(2) .box-card-content{
          background: #F3FAFF;
          span:last-child{
            color: #3EAAF9;
          }
        }
        &:nth-child(3) .box-card-content{
          background: #FFF8F1;
          span:last-child{
            color: #F08628;
          }
        }
        &:nth-child(4) .box-card-content{
          background: #F4FFE7;
          span:last-child{
            color: #82BC3C;
          }
        }
        &:nth-child(5) .box-card-content{
          background: #F8F5FF;
          span:last-child{
            color: #966AF9;
          }
        }
      }
      .current-position-name {
        font-size: 18px;
        font-weight: bold;
        margin-bottom: 10px;
      }
    }

		.course-search {
			background-color: #fff;
			padding: 0 10px;

			.svg-container {
				padding: 0 5px 0 10px;
				color: $dark_gray;
				vertical-align: middle;
				display: inline-block;

				&_login {
					font-size: 20px;
				}
			}

			.search-input {
				width: 60%;
				position: relative;

				&:after {
					content: "";
					position: absolute;
					left: -25px;
					right: 0;
					bottom: 8px;
					height: 1px;
					background: rgba(226, 227, 232, 1);
				}

				input {
					outline: none;
					background: transparent;
					border: none;
					outline: medium;
					padding: 0;

					&:focus {
						outline: none;
						background-color: transparent;
					}
				}
			}

			.course-search-btn {
				display: inline-block;
				float: right;

				.el-button.is-round {
					padding: 10px 30px;
					margin-right: 34px;
				}

				.course-search-more {
					font-size: 14px;
					font-weight: 500;
					@include font-color($mainColor);
					cursor: pointer;
					margin-left: 10px;
				}
			}

			&.merge {
				height: 50px;
				line-height: 50px;
			}

			&.separate {
				.course-search-inner {
					padding-top: 10px;

					// .el-input--medium .el-input__inner {
					// 	padding-left: 30px !important;
					// }
					.search-inner-item {
						.item-label {
							height: 30px;
							line-height: 30px;
							font-size: 14px;
							color: #a6a6a6;
							font-weight: 400;
						}

						.el-input {
							position: relative;

							.el-input__inner {
								border: none;
								padding: 0;
							}

							&:after {
								content: "";
								height: 1px;
								background-color: #e2e3e8;
								position: absolute;
								left: 0;
								right: 0;
								bottom: 0;
							}
						}
					}
				}

				.course-search-btn {
					text-align: center;
					display: block;
					float: none;
					height: 56px;
					line-height: 56px;
				}
			}
		}

		.main-buttons {
			display: flex;
			line-height: 36px;
			margin: 10px 0;

			.main-buttons-title {
				flex: 1;
				height: 36px;
				line-height: 36px;
				font-size: 16px;
				font-weight: bold;
				color: rgba(51, 51, 51, 1);

				.num {
					@include font-color($mainColor);
				}
			}
		}
	}
</style>
